/**
 * 作者： 天天修改
 * github: webkixi
 * 小程序的模板真是又长又臭
 */
 const app = null //getApp()
 const Core = require('../aotoo/core/index')
 const lib = Core.lib
 

 const adapter = (params) => {
    let countSum = 0
    let headData = params.dataSource.headData
    headData.map(item => {
      item.width = item.width || 200
      if (item.width) {
        countSum += parseInt(item.width)
      }
    })
    params.dataSource.data.map(item => {
      for(let i = 0; i < headData.length; i++) {
        if ( headData[i].formatter) {
          const output = headData[i].formatter.call(this, item)
          item[headData[i].key] = output
        }
      }
    })
    if (countSum !== 0) {
      params.dataSource.tableStyle = '--width: ' + countSum + params.unit
    }
    // params.headData = params.dataSource.headData
    // params.data = params.dataSource.data
    // params.tableClass = params.dataSource.tableClass
    // params.unit = params.dataSource.unit
    // params.onItemClick = params.dataSource.onItemClick
    // params.暂无数据 = params.dataSource.暂无数据
    return params.dataSource
 }

 Component({
   options: {
     multipleSlots: true, // 在组件定义时的选项中启用多slot支持
     addGlobalClass: true
   },
   behaviors: [],
   properties: {
     dataSource: Object
   },

  //  $dataSource: {},
   
   data: {
     id: '',
     headData: [],
     data: [],
     tableClass: '',
     unit: 'rpx',
     tableStyle: '',
     onItemClick: null,
     noDataText: '暂无数据'
   }, // 私有数据，可用于模板渲染
 
   lifetimes: {
     // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
     created(){
     },
     attached: function () {
        let dataSource = this.properties.dataSource
        // console.log(this.properties, 'dataSource')
        const {id, headData, data, tableClass, unit, tableStyle, onItemClick, noDataText} = adapter(this.properties)
        if (data.length > 0) {
          data
        }
        this.setData({
          id: id || lib.suid('table-'),
          headData: headData,
          data: data,
          tableClass: tableClass,
          unit: unit,
          tableStyle: tableStyle,
          onItemClick: onItemClick,
          noDataText: noDataText
        })
       
     },
     moved: function () { },
     detached: function () {},
   },
 
   methods: {
    hasItemClick(e, params, inst) {
      const {value, idx, key} = e.currentTarget.dataset
      if (typeof this.data.onItemClick === 'function') {
        this.data.onItemClick.call(this, e.currentTarget.dataset)
      }
      // const instTd = this.find({id: key+'_'+idx})
      // return newCfg.onItemClick.call(instTd, params)
    },
   }
 });